package com.mjh.zhujianju.controller.set;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.livew.framework.base.controller.BaseController;
import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.BaseService;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.enums.EUserRoleType;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedResult;
import com.livew.framework.pojo.AjaxResult;
import com.livew.framework.utils.XTListUtils;
import com.livew.framework.utils.XTStringUtils;
import com.mjh.zhujianju.service.set.AdminService;
import com.mjh.zhujianju.vo.base.SysUserVO;

@Controller
@RequestMapping("set/admin")
public class AdminController extends BaseController<SysUser> {

	@Resource
	private AdminService adminService;

	@Resource
	private SysUserService userService;

	@Override
	protected BaseService<SysUser> getService() {
		return adminService;
	}

	/**
	 * 保存
	 * 
	 * @Description:
	 * @param vo
	 * @return
	 * @author wt1293
	 * @date 2018年8月21日下午1:46:27
	 *
	 */
	@RequestMapping(value = "saveInfo", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult saveInfo(SysUserVO vo) {
		if (vo == null||XTStringUtils.isEmpty(vo.getUsername())||XTStringUtils.isEmpty(vo.getPassword())) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		try {
			adminService.saveInfo(vo);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(Boolean.TRUE, "保存成功");
	}

	/**
	 * 
	 * @Description: 更新
	 * @param vo
	 * @return
	 * @author wt1293
	 * @date 2018年8月21日下午1:46:14
	 *
	 */

	@RequestMapping(value = "updateInfo", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult updateInfo(SysUserVO vo) {
		if (vo == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		try {
			adminService.updateInfo(vo);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(Boolean.TRUE, "保存成功");
	}

	/**
	 * 分页
	 * 
	 * @Description:
	 * @return
	 * @author wt1293
	 * @date 2018年8月9日下午8:08:53
	 *
	 */
	@RequestMapping(value = "getPage", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult getPage(SysUser vo) {
		vo.setType(EUserRoleType.message.getCode());
		PagedResult<SysUserVO> list = adminService.getVOPage(vo);
		return AjaxResult.getResult(list);
	}

	/**
	 * 获取详情
	 */
	public AjaxResult findById(@PathVariable("id") Long id) {
		if (id == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		SysUserVO t = null;
		try {
			t = this.adminService.getInfo(id);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(t);
	}

	@RequestMapping(value = "checkUsername",method =RequestMethod.POST)
	@ResponseBody
	public Boolean checkUsername(SysUser vo) {
		List<SysUser> list = userService.getListByCondition(vo);
		if (XTListUtils.isEmpty(list)) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}
}
